package chapter10;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import chapter10.mapper.StudentMapper;
import chapter10.po.Student;
public class BatisChapter10Test {
    private static SqlSessionFactory sqlSessionFactory = null;
    // 初始化SqlSessionFactory对象
    static {
        try {
            // 使用MyBatis提供的Resources类加载MyBatis的配置文件
            Reader reader =
                    Resources.getResourceAsReader("chapter10_batis.xml");
            // 构建SqlSessionFactory工厂
            sqlSessionFactory =
                    new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 获取SqlSession对象的静态方法
    public static SqlSession getSession() {
        return sqlSessionFactory.openSession();
    }

    /**
     * 1.根据学号查询学生信息
     */

    public void findStudentByStunoTest() {
        // 获取SqlSession
        SqlSession sqlSession = getSession();
        // SqlSession执行映射文件中定义的SQL，并返回映射结果
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student stu = mapper.findStudentByStuno(2018020222);
        // 打印输出结果
        System.out.println(stu.toString());
        // 关闭SqlSession
        sqlSession.close();
    }

    /**
     * 2.查询全部学生信息
     */

    public void findStudentListTest() {
        // 获取SqlSession
        SqlSession sqlSession = getSession();
        // SqlSession执行映射文件中定义的SQL，并返回映射结果
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> stus = mapper.findStudentList();
        // 打印输出结果
        System.out.println(stus.toString());
        // 关闭SqlSession
        sqlSession.close();
    }

    /**
     * 3.添加学生信息
     */
    public void addStudentTest(){
        // 获取SqlSession
        SqlSession sqlSession = getSession();
        Student stu = new Student();
        stu.setStuno(2019040101);
        stu.setStuname("赵六");
        stu.setGrade(2019);
        stu.setDept("日语");
        stu.setClassname("日语1901");
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        // 执行sqlSession的插入方法，返回的是SQL语句影响的行数
        int rows = mapper.insertStudent(stu);
        if(rows > 0){
            System.out.println("您成功插入了"+rows+"条数据！");
        }else{
            System.out.println("执行插入操作失败！！！");
        }
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 4.更新客户
     */
    public void updateStudentTest() throws Exception{
        // 获取SqlSession
        SqlSession sqlSession = getSession();
        // SqlSession执行更新操作
        // 创建Student对象，并向对象中添加数据
        Student stu = new Student();
        stu.setStuno(2019040101);
        stu.setDept("英语");
        stu.setClassname("英语1901");
        // 执行sqlSession的更新方法，返回的是SQL语句影响的行数
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int rows = mapper.updateStudent(stu);
        // 通过返回结果判断更新操作是否执行成功
        if(rows > 0){
            System.out.println("您成功修改了"+rows+"条数据！");
        }else{
            System.out.println("执行修改操作失败！！！");
        }
        // 提交事务
        sqlSession.commit();
        // 关闭SqlSession
        sqlSession.close();
    }
    /**
     * 5.删除客户
     */
    public void deleteStudentTest() {
        // 获取SqlSession
        SqlSession sqlSession = getSession();
        // SqlSession执行删除操作
        // 执行SqlSession的删除方法，返回的是SQL语句影响的行数
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        int rows = mapper.deleteStudent(2019040101);
        // 通过返回结果判断删除操作是否执行成功
        if(rows > 0){
            System.out.println("您成功删除了"+rows+"条数据！");
        }else{
            System.out.println("执行删除操作失败！！！");
        }
        // 提交事务
        sqlSession.commit();
        // 关闭SqlSession
        sqlSession.close();
    }

    public static void main(String[] args) {
        BatisChapter10Test test=new BatisChapter10Test();
        test.findStudentByStunoTest();
        test.findStudentListTest();
        try {
            test.addStudentTest();
            test.updateStudentTest();
            test.deleteStudentTest();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
